package za.um.td.util.texture;

import java.net.URL;

import za.um.td.entity.level.Terrain;
import za.um.td.state.BasicGameStateSingleton;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureKey;
import com.jme.util.TextureManager;

public class Texturete
{
	private String id = null;
	private int scale = 0;
	private int order = 0;

	private TextureKey textureKey = null;
	private TextureKey textureKey_defuse = null;
	private TextureKey textureKey_normal = null;
	private TextureKey textureKey_specular = null;
	
	
	public final static String NODE_TEXTURE = "texture";
	public final static String ATRI_NAME = "name";
	public final static String ATRI_SCALE = "scale";
	public final static String ATRI_DEFUSE = "diffuse";
	public final static String ATRI_NORMAL = "normal";
	public final static String ATRI_SPECULAR = "specular";
	
	
	
	public Texturete(String id)
	{
		this.setId(id);
	}
	
	public void init(String tex)
	{
		textureKey = loadTexture(tex);
	}
	
	public void init(String tex, String tex_defuse)
	{
		textureKey = loadTexture(tex);
		textureKey_defuse = loadTexture(tex_defuse);
	}
	
	public void init(String tex, String tex_defuse, String tex_normal)
	{
		textureKey = loadTexture(tex);
		textureKey_defuse = loadTexture(tex_defuse);
		textureKey_normal = loadTexture(tex_normal);
	}
	
	public void init(String tex, String tex_defuse, String tex_normal, String tex_specular)
	{
		textureKey = loadTexture(tex);
		textureKey_defuse = loadTexture(tex_defuse);
		textureKey_normal = loadTexture(tex_normal);
		textureKey_specular = loadTexture(tex_specular);
	}
	
	public static TextureKey loadTexture(String textureURL)
	{
		com.jme.image.Texture t0 = TextureManager.loadTexture(Terrain.class
                .getClassLoader().getResource(textureURL),
                com.jme.image.Texture.MinificationFilter.Trilinear,
                com.jme.image.Texture.MagnificationFilter.Bilinear);
		
		TextureManager.addToCache(t0);
		
		return t0.getTextureKey();
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getScale() {
		return scale;
	}
	public void setScale(int scale) {
		this.scale = scale;
	}
	
	public com.jme.image.Texture getTexture()
	{
		return TextureManager.findCachedTexture(textureKey);
	}
	
	public void setTexture(com.jme.image.Texture texture)
	{
		textureKey = texture.getTextureKey();
		com.jme.image.Texture t0 = TextureManager.findCachedTexture(textureKey);
		if(t0 == null)
			TextureManager.addToCache(texture);
	}
	
	public com.jme.image.Texture getTextureDefuse()
	{
		return TextureManager.findCachedTexture(textureKey_defuse);
	}
	
	public void setTextureDefuse(com.jme.image.Texture texture)
	{
		textureKey_defuse = texture.getTextureKey();
		com.jme.image.Texture t0 = TextureManager.findCachedTexture(textureKey_defuse);
		if(t0 == null)
			TextureManager.addToCache(texture);
	}
	
	public com.jme.image.Texture getTextureNormal()
	{
		return TextureManager.findCachedTexture(textureKey_normal);
	}
	
	public void setTextureNormal(com.jme.image.Texture texture)
	{
		textureKey_normal = texture.getTextureKey();
		com.jme.image.Texture t0 = TextureManager.findCachedTexture(textureKey_normal);
		if(t0 == null)
			TextureManager.addToCache(texture);
	}
	
	public com.jme.image.Texture getTextureSpecular()
	{
		return TextureManager.findCachedTexture(textureKey_specular);
	}
	
	public void setTextureSpecular(com.jme.image.Texture texture)
	{
		textureKey_specular = texture.getTextureKey();
		com.jme.image.Texture t0 = TextureManager.findCachedTexture(textureKey_specular);
		if(t0 == null)
			TextureManager.addToCache(texture);
	}
	
	public static boolean isCached(Texture texture)
	{
		if(texture!=null && texture.getTextureKey()!=null)
			if(TextureManager.findCachedTexture(texture.getTextureKey()) == null)
				return false;
		return true;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
	
	public TextureState createSplatTextureState(Vector3f scale, String alpha) {
		DisplaySystem display = BasicGameStateSingleton.game.getDisplay();
        TextureState ts = display.getRenderer().createTextureState();

        Texture t0 = this.getTexture();
        
        t0.setWrap(Texture.WrapMode.Repeat);
        t0.setApply(Texture.ApplyMode.Modulate);
        t0.setScale(scale);
        ts.setTexture(t0, 0);

        if (alpha != null) {
            addAlphaSplat(ts, alpha);
        }

        return ts;
    }
	
	public static void addAlphaSplat(TextureState ts, String alpha) {
        Texture t1 = TextureManager.loadTexture(Terrain.class
                .getClassLoader().getResource(alpha),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear);
        t1.setWrap(Texture.WrapMode.Repeat);
        t1.setApply(Texture.ApplyMode.Combine);
        t1.setCombineFuncRGB(Texture.CombinerFunctionRGB.Replace);
        t1.setCombineSrc0RGB(Texture.CombinerSource.Previous);
        t1.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);
        t1.setCombineFuncAlpha(Texture.CombinerFunctionAlpha.Replace);
        ts.setTexture(t1, ts.getNumberOfSetTextures());
    }
	
	public static TextureState createLightmapTextureState(String texture) {
		DisplaySystem display = BasicGameStateSingleton.game.getDisplay();
        TextureState ts = display.getRenderer().createTextureState();

        Texture t0 = TextureManager.loadTexture(Terrain.class
                .getClassLoader().getResource(texture),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear);
        t0.setWrap(Texture.WrapMode.Repeat);
        ts.setTexture(t0, 0);

        return ts;
    }

}
